import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class PhoneRecycleApp extends JFrame {
    private CardLayout cardLayout;
    private JPanel cardPanel;

    // 页面常量
    private static final String HOME_PAGE = "home";
    private static final String VALUATION_PAGE = "valuation";
    private static final String CONFIRMATION_PAGE = "confirmation";
    private static final String SUCCESS_PAGE = "success";

    public PhoneRecycleApp() {
        // 设置窗口属性
        setTitle("二易集 - 手机回收平台");
        setSize(400, 700);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);

        // 创建卡片布局
        cardLayout = new CardLayout();
        cardPanel = new JPanel(cardLayout);

        // 创建各个页面
        HomePage homePage = new HomePage(this);
        ValuationPage valuationPage = new ValuationPage(this);
        ConfirmationPage confirmationPage = new ConfirmationPage(this);
        SuccessPage successPage = new SuccessPage(this);
        
        cardPanel.add(homePage, HOME_PAGE);
        cardPanel.add(valuationPage, VALUATION_PAGE);
        cardPanel.add(confirmationPage, CONFIRMATION_PAGE);
        cardPanel.add(successPage, SUCCESS_PAGE);

        // 添加卡片面板到窗口
        add(cardPanel);

        // 显示首页
        cardLayout.show(cardPanel, HOME_PAGE);
    }

    // 页面切换方法
    public void showPage(String pageName) {
        cardLayout.show(cardPanel, pageName);
    }

    // 创建通用卡片面板
    public static JPanel createCardPanel(String title) {
        JPanel card = new JPanel();
        card.setLayout(new BoxLayout(card, BoxLayout.Y_AXIS));
        card.setBackground(Color.WHITE);
        card.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
        card.setAlignmentX(Component.LEFT_ALIGNMENT);
        
        if (title != null) {
            JLabel titleLabel = new JLabel(title);
            titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 14));
            titleLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
            card.add(titleLabel);
            card.add(Box.createVerticalStrut(10));
        }
        
        return card;
    }

    // 创建通用头部
    public static JPanel createHeader(String title, String backPage, PhoneRecycleApp app) {
        JPanel header = new JPanel(new BorderLayout());
        header.setBackground(Color.WHITE);
        header.setPreferredSize(new Dimension(400, 50));
        
        // 返回按钮
        JButton backButton = new JButton("←");
        backButton.setFont(new Font("Arial", Font.PLAIN, 16));
        backButton.setContentAreaFilled(false);
        backButton.setBorderPainted(false);
        backButton.addActionListener(e -> app.showPage(backPage));
        header.add(backButton, BorderLayout.WEST);
        
        // 标题
        JLabel titleLabel = new JLabel(title);
        titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
        header.add(titleLabel, BorderLayout.CENTER);
        
        // 关闭按钮
        JButton closeButton = new JButton("✕");
        closeButton.setFont(new Font("Arial", Font.PLAIN, 14));
        closeButton.setContentAreaFilled(false);
        closeButton.setBorderPainted(false);
        closeButton.addActionListener(e -> app.showPage(backPage));
        header.add(closeButton, BorderLayout.EAST);
        
        return header;
    }

    public static void main(String[] args) {
        // 使用SwingUtilities确保GUI线程安全
        SwingUtilities.invokeLater(() -> {
            PhoneRecycleApp app = new PhoneRecycleApp();
            app.setVisible(true);
        });
    }
}